package com.itany.corejava.day05;

import java.util.Arrays;

public class Test4 {

    public static void main(String[] args) {
        // int[] a={5,3,1,2,4};

        int[] a=new int[599990];

        for (int i = 0; i < a.length; i++) {
            a[i]= (int) (Math.random()*1000000000);
        }

        long start=System.currentTimeMillis();


        // selectSort(a);//8870

        // bubleSort(a);//7483

        // insertSort(a);//687

        Arrays.sort(a);//21

        long end=System.currentTimeMillis();

        System.out.println(end-start);

        // System.out.println(Arrays.toString(a));
    }

    public static void selectSort(int[] a){
        for (int i = 0; i < a.length-1; i++) {
            for (int j = i+1; j < a.length; j++) {
                if (a[i]>a[j]){
                    a[i]=a[i]+a[j];
                    a[j]=a[i]-a[j];
                    a[i]=a[i]-a[j];
                }
            }
        }
    }

    public static void bubleSort(int[] a){
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-i-1; j++) {
                if(a[j]>a[j+1]){
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }

    public static void insertSort(int[] a){
        for (int i = 1; i < a.length; i++) {
            int n=a[i];
            int j=i-1;
            while(j!=-1&&n<a[j]){
                a[j+1]=a[j];
                j--;
            }
            a[j+1]=n;
        }
    }
}
